package com.zhuzhoujia.city_wide_action;

import java.util.List;
import java.util.Random;

import com.example.zhuzhoujia.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ListActivityAdapter extends BaseAdapter {
	private Context context;
	private List<String> list;
	
	
	
	public ListActivityAdapter(Context context) {
		super();
		this.context = context;
	}

	@ViewInject(R.id.activity_image1)
	private ImageView imageView1;
	@ViewInject(R.id.activity_image2)
	private ImageView imageView2;
	@ViewInject(R.id.time)
	private LinearLayout layout;
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 6;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Random random = new Random();
		int rdm = random.nextInt(100);
		if (rdm % 3 == 0) {
			View v = inflateView(context, R.layout.activity_type1);
			ViewUtils.inject(this, v);
			return v;
		} else if (rdm % 3 == 1) {
			View v = inflateView(context, R.layout.activity_type2);
			ViewUtils.inject(this, v);
			setImageW(layout, 0.382);
			setImage(imageView1, R.drawable.test, context,0.618);
			return v;
		} else {
			View v = inflateView(context, R.layout.activity_type3);
			ViewUtils.inject(this, v);
			setImage(imageView1, R.drawable.test, context,0.5);
			setImage(imageView2, R.drawable.test1, context,0.5);
			return v;
			}
	}
	
	public View inflateView(Context context, int resource) {
		View v = LayoutInflater.from(context).inflate(resource, null);
		return v;
	}
	
	
	
	public float getDisplayDensity(Context context) {
		return context.getResources().getDisplayMetrics().density;
	}
	
	
	public void setImageW(View view, double d2) {
		float d = getDisplayDensity(context);
		int w = (int) ((getDisplayWidth(context) - 41 * d) * d2);

		LayoutParams lp = view.getLayoutParams();
		lp.width = w;
		view.setLayoutParams(lp);
	}
	
	public void setImageH(View view, double d2) {
		float d = getDisplayDensity(context);
		int h = (int) ((getDisplayWidth(context) - 41 * d) * d2 * 9 / 16);

		LayoutParams lp = view.getLayoutParams();
		lp.height = h;
		view.setLayoutParams(lp);
	}
	
	public void setImageWH(View view, double d2) {
		float d = getDisplayDensity(context);
		int w = (int) ((getDisplayWidth(context) - 41 * d) * d2);
		int h = (int) ((getDisplayWidth(context) - 41 * d) * d2 * 9 / 16);

		LayoutParams lp = view.getLayoutParams();
		lp.height = h;
		lp.width = w;
		view.setLayoutParams(lp);
	}
	
	public int getDisplayWidth(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		return dm.widthPixels;
	}

	public void setImage(ImageView view, int resource, Context context, double d) {
		setImageWH(view, d);
		view.setBackground(context.getResources().getDrawable(resource));
	}
}
